<template>
    <div>
        <div class="login">
            <div class="bg">
                <h3>Kueiq音乐 管理员登陆</h3>
                <el-card class="login-card">
                    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="60px"
                             class="demo-ruleForm" hide-required-asterisk>
                        <el-form-item label="管理员" prop="name">
                            <el-input type="text" v-model="ruleForm.name" autocomplete="off"></el-input>
                        </el-form-item>
                        <el-form-item label="密码" prop="pass">
                            <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
                            <el-button @click="resetForm('ruleForm')">重置</el-button>
                        </el-form-item>
                    </el-form>
                </el-card>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        name: "H-login",
        data() {
            let validateName = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('登录名不能为空'));
                } else {
                    callback();
                }
            };
            let reg = /(?!^(\d+|[a-zA-Z]+|[~!@#$%^&*?]+)$)^[\w~!@#$%^&*?]{6,16}$/;
            let validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else if (!reg.test(value)) {
                    callback(new Error('密码应是6-12位数字、字母或字符！'))
                    // if (this.ruleForm.checkPass !== '') {
                    //     this.$refs.ruleForm.validateField('checkPass');
                    // }
                } else {
                    callback();
                }
            };
            return {
                ruleForm: {
                    pass: '',
                    name: '',

                },
                rules: {
                    name: [
                        {type: 'string', required: true, message: '请输入登录名'},
                        {min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur'},
                        {
                            required: true,
                            pattern: /^[\u4e00-\u9fa5_a-zA-Z0-9.·-]+$/,
                            message: '姓名不支持特殊字符',
                            trigger: 'blur'
                        },
                        {validator: validateName, trigger: 'blur'}
                    ],
                    pass: [
                        {required: true, message: '请输入密码'},
                        {validator: validatePass, trigger: 'blur'}
                    ]
                }
            };
        },

        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        if (this.ruleForm.name === "admin" && this.ruleForm.pass === "z123456") {
                            this.$notify({
                                type: 'success',
                                message: "欢迎管理员" + this.ruleForm.name,
                                duration: 3000
                            })
                            // 登录成功
                            // 1. 存储 token
                            localStorage.setItem('token', 'Bearer xxxx')
                            // 2. 跳转到后台主页
                            this.$router.replace("/Shuju")
                        } else {
                            this.$message({
                                type: "error",
                                message: "密码错误或用户名错误",
                                showClose: true
                            }),
                                // 登录失败
                                localStorage.removeItem('token')
                        }
                        // alert('submit!');
                    } else {
                        console.log('error submit!!');
                        return false;
                    }

                });
            },

            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }

    }
</script>

<style scoped>
    .login {
        width: 100%;
        height: 100%;
        background-image: url("./../img/2222.png");
        background-size: cover;
        position: absolute;
        background-repeat: no-repeat;
        z-index: -1;
    }

    .login .bg {
        position: absolute;
        margin: auto;
        top: 50%;
        left: 50%;
        width: 500px;
        height: 480px;
        transform: translate(-50%, -50%);
    }

    .login .bg h3 {
        text-align: center;
        color: #ffffff;
        font-size: 2.4em;
        letter-spacing: 3px;
        text-shadow: 2px 2px 5px #cccccc;
    }

    .login-card {
        position: absolute;
        margin: auto;
        width: 450px;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        padding: 45px 45px;
        border-radius: 3%;
    }
</style>
